using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Service.ProdManage.IProdManageService;

namespace ZR.Service.ProdManage
{
    /// <summary>
    /// 一级装箱明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IProdOnePackDetailService), ServiceLifetime = LifeTime.Transient)]
    public class ProdOnePackDetailService : BaseService<ProdOnePackDetail>, IProdOnePackDetailService
    {
        /// <summary>
        /// 查询一级装箱明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ProdOnePackDetailDto> GetPageList(ProdOnePackDetailQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<ProdOnePackDetailDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<ProdOnePack>((dtl, pack) => dtl.OnePackId == pack.PackId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((dtl, pack) => new ProdOnePackDetailDto
                {
                    DetailId = dtl.DetailId,
                    OnePackId = dtl.OnePackId,
                    ItemStatus = dtl.ItemStatus,
                    OrgCode = dtl.OrgCode,
                    PackNum = dtl.PackNum,
                    PartCode = dtl.PartCode,
                    SupplierCode = dtl.SupplierCode,
                    PackUserId = dtl.PackUserId,
                    PackTime = dtl.PackTime,
                    OnePackNo = pack.PackNo
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="DetailId"></param>
        /// <returns></returns>
        public ProdOnePackDetail GetInfo(string DetailId)
        {
            var response = Queryable()
                .Where(x => x.DetailId == DetailId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加一级装箱明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ProdOnePackDetail AddProdOnePackDetail(ProdOnePackDetail model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改一级装箱明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateProdOnePackDetail(ProdOnePackDetail model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<ProdOnePackDetail> QueryExp(ProdOnePackDetailQueryDto parm)
        {
            var predicate = Expressionable.Create<ProdOnePackDetail>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.OnePackId), it => it.OnePackId == parm.OnePackId);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.OrgCode), it => it.OrgCode == parm.OrgCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PartCode), it => it.PartCode == parm.PartCode);
            predicate = predicate.AndIF(parm.BeginPackTime != null, it => it.PackTime >= parm.BeginPackTime);
            predicate = predicate.AndIF(parm.EndPackTime != null, it => it.PackTime <= parm.EndPackTime);
            return predicate;
        }
    }
}